package com.dong.order.service.impl;

import com.dong.order.feign.UserFeign;
import com.dong.order.pojo.Order;
import com.dong.order.pojo.User;
import com.dong.order.service.OrderService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author dongjian
 */
@Service
@AllArgsConstructor
public class OrderServiceImpl implements OrderService {

    @Autowired
    RestTemplate restTemplate;

    UserFeign userFeign;

    @Override
    public Order getOrder(Long orderId) {
        Order order = new Order()
                .setOrderId(2L)
                .setPrice(new BigDecimal("100"))
                .setUserId(1L);
        // 查询user
//        String url = "http://userservice/user/" + order.getUserId();
        // 发送http请求，实现远程调用
//        User user = restTemplate.getForObject(url, User.class);
        // 通过Feign远程调用其他微服务接口
        User user = userFeign.findUserById(order.getUserId());
        // 封装user到order
        order.setUser(user);
        return order;
    }
}
